import {Component, Input, OnInit} from "@angular/core";
import {LocalDataSource} from "ng2-smart-table";
import {NbDateService} from "@nebular/theme";
import {BaseRequest} from "../../utils/BaseRequest";
import {map} from "rxjs/operators";
import {PowerExtraService} from "../../energy/api/api/powerExtra.service";
import {ResponseDailyElectricConsumptionData} from "../../energy/api/model/responseDailyElectricConsumptionData";

@Component({
  selector: 'device-daily-component',
  template:`
      <ng2-smart-table [settings]="settings" [source]="source">
      </ng2-smart-table>
  `
})
export class DeviceDailyComponent implements OnInit {
  @Input('devNo') devNo;
  settings = {
    pager: {
      display: true,
      perPage: 20
    },
    hideSubHeader: true,
    actions: {
      add: false,
      delete: false,
      edit: false,
      position: 'right'
    },
    columns: {
      devNo: {
        title: '设备编号',
        type: 'string',
      },
      isPooled: {
        title: '是否公摊',
        type: 'string',
      },
      beginMeterReading: {
        title: '起始表值',
        type: 'number',
      },
      endMeterReading: {
        title: '结束表值',
        type: 'number',
      },
      dataTime: {
        title: '产生时间',
        type: 'string',
      },
      totalConsumption: {
        title: '总计消耗',
        type: 'number',
      },
      totalPositiveMeterReading: {
        title: '正总表码',
        type: 'number',
      },
      tipPositiveMeterReading: {
        title: '正尖表码',
        type: 'number',
      },
      peakPositiveMeterReading: {
        title: '正峰表码',
        type: 'number',
      },
      plainPositiveMeterReading: {
        title: '正平表码',
        type: 'number',
      },
      valleyPositiveMeterReading: {
        title: '正谷表码',
        type: 'number',
      },
      totalReverseMeterReading: {
        title: '负总表码',
        type: 'number',
      },
      tipReverseMeterReading: {
        title: '负尖表码',
        type: 'number',
      },
      peakReverseMeterReading: {
        title: '负峰表码',
        type: 'number',
      },
      plainReverseMeterReading: {
        title: '负平表码',
      },
      valleyReverseMeterReading: {
        title: '负谷表码',
        type: 'number',
      },
      totalPositiveActivePower: {
        title: '总正向有功',
        type: 'number',
      },
      tipPositiveActivePower: {
        title: '正向有功尖',
        type: 'number',
      },
      peakPositiveActivePower: {
        title: '正向有功峰',
        type: 'number',
      },
      plainPositiveActivePower: {
        title: '正向有功平',
        type: 'number',
      },
      valleyPositiveActivePower: {
        title: '正向有功谷',
        type: 'number',
      },
      totalReverseActivePower: {
        title: '反向有功总',
        type: 'number',
      },
      totalReverseReactivePower: {
        title: '反向无功总',
        type: 'number',
      }
    }
  };
  source: DailyDataDataSource;
  startTime: Date = new Date();
  endTime: Date = new Date();

  constructor(private api: PowerExtraService, private dateService: NbDateService<Date>) {
    this.startTime.setDate(0);

  }

  ngOnInit(): void {
    this.source = new DailyDataDataSource(this.api, this.dateService, this.devNo, this.startTime, this.endTime);
  }
}

export class DailyDataDataSource extends LocalDataSource {
  all: number = -1;
  constructor(private api: PowerExtraService, private dateService: NbDateService<Date>
    , public devNo?: string
    , public startTime?: Date
    , public endTime?: Date, private handler?: any) {
    super();
    this.pagingConf['page'] = 1;
    this.pagingConf['perPage'] = 20;
  }

  count(): number {
    return this.all;
  }

  getElements(): Promise<any> {
    const query = {
      pageIndex: (this.pagingConf['page'] - 1) * this.pagingConf['perPage'],
      pageSize: this.pagingConf['perPage'],
      devNo: this.devNo,
    };
    if (this.startTime != null)
      query['startTime'] = this.dateService.format(this.startTime, "yyyyMMdd");
    if (this.endTime != null)
      query['endTime'] = this.dateService.format(this.endTime, "yyyyMMdd");

    return this.api.requestDailyElectrcConsumptionData(new BaseRequest(query)).pipe(map((res: ResponseDailyElectricConsumptionData) => {
      this.all = res.dataCount;
      if (this.handler != null) this.handler(res.data);
      return res.data;
    })).toPromise();
  }
}
